package com.example.ggpicturegg.Mysql_com;

import android.util.Log;

import com.example.ggpicturegg.imageinfo.UserNameAndImageInfo;
import com.example.ggpicturegg.imageinfo.UserNameAndNameinfo;
import com.mysql.jdbc.PreparedStatement;

public class Mysql_tool extends DbOpenHelper{

    public UserNameAndImageInfo find_is_like(String name,int id){

        UserNameAndImageInfo item = null;
        try{
            getConnection();
            String sql="select * from likeinfo where user_like_name=? and image_like_id=?";
            pStmt = (PreparedStatement) conn.prepareStatement(sql);
            pStmt.setString(1,name);
            pStmt.setInt(2,id);
            rs = pStmt.executeQuery();
            if(rs.next()){
                item = new UserNameAndImageInfo();
                item.setName(name);
                item.setId(id);
            }

        }catch (Exception e){
            Log.d("------find_is_like-----", "   is     -----------          error  ");
            e.printStackTrace();
        }finally {
            clossAll();  //关闭所有连接
        }

        System.out.println("-------------  " + item==null + "  ---------------------------");
        return item;
    }

    public UserNameAndNameinfo find_is_care(String care_name, String be_care_name){

        UserNameAndNameinfo item = null;
        try{
            getConnection();
            String sql="select * from careinfo where user_care_name1=? and user_care_name2=?";
            pStmt = (PreparedStatement) conn.prepareStatement(sql);
            pStmt.setString(1,care_name);
            pStmt.setString(2,be_care_name);
            rs = pStmt.executeQuery();
            if(rs.next()){
                item = new UserNameAndNameinfo();
                item.setBe_care_name(be_care_name);
                item.setCare_name(care_name);
            }

        }catch (Exception e){
            Log.d("------find_is_care-----", "   is     -----------          error ");
            e.printStackTrace();
        }finally {
            clossAll();  //关闭所有连接
        }

        return item;
    }

    public int delete_is_care(String care_name, String be_care_name){
        int Row = 0;
        try{
            getConnection();
            String sql="delete from careinfo where user_care_name1=? and user_care_name2=?";
            pStmt = (PreparedStatement) conn.prepareStatement(sql);
            pStmt.setString(1,care_name);
            pStmt.setString(2,be_care_name);
            Row = pStmt.executeUpdate();
        }catch (Exception e){
            Log.d("------find_is_care-----", "   is     -----------          error ");
            e.printStackTrace();
        }finally {
            clossAll();  //关闭所有连接
        }
        return Row;
    }

    public int add_is_care(String care_name, String be_care_name){

        int Row = 0;
        try{
            getConnection();
            String sql="insert into careinfo(user_care_name1,user_care_name2) value(?,?)";
            pStmt = (PreparedStatement) conn.prepareStatement(sql);
            pStmt.setString(1,care_name);
            pStmt.setString(2,be_care_name);
            Row = pStmt.executeUpdate();

        }catch (Exception e){
            Log.d("------find_is_care-----", "   is     -----------          error ");
            e.printStackTrace();
        }finally {
            clossAll();  //关闭所有连接
        }

        return Row;
    }


    public int delete_is_like(String care_name, int id){
        int Row = 0;
        try{
            getConnection();
            String sql="delete from likeinfo where user_like_name=? and image_like_id=?";
            pStmt = (PreparedStatement) conn.prepareStatement(sql);
            pStmt.setString(1,care_name);
            pStmt.setInt(2,id);
            Row = pStmt.executeUpdate();
        }catch (Exception e){
            Log.d("------find_is_care-----", "   is     -----------          error ");
            e.printStackTrace();
        }finally {
            clossAll();  //关闭所有连接
        }
        return Row;
    }

    public int add_is_like(String care_name, int id){

        int Row = 0;
        try{
            getConnection();
            String sql="insert into likeinfo(user_like_name,image_like_id) value(?,?)";
            pStmt = (PreparedStatement) conn.prepareStatement(sql);
            pStmt.setString(1,care_name);
            pStmt.setInt(2,id);
            Row = pStmt.executeUpdate();

        }catch (Exception e){
            Log.d("------find_is_care-----", "   is     -----------          error ");
            e.printStackTrace();
        }finally {
            clossAll();  //关闭所有连接
        }

        return Row;
    }

    public int add_name_image(String name ,String image){

        int Row = 0;
        try{
            getConnection();
            String sql="insert into image(name,imagename) value(?,?)";
            pStmt = (PreparedStatement) conn.prepareStatement(sql);
            pStmt.setString(1,name);
            pStmt.setString(2,image);
            Row = pStmt.executeUpdate();

        }catch (Exception e){
            Log.d("------insert image-----", "   is     -----------          error ");
            e.printStackTrace();
        }finally {
            clossAll();  //关闭所有连接
        }

        return Row;
    }

    public int delete_name_image(String name){
        int Row = 0;
        try{
            getConnection();
            String sql="delete from image where name=?";
            pStmt = (PreparedStatement) conn.prepareStatement(sql);
            pStmt.setString(1,name);
            Row = pStmt.executeUpdate();
            Log.d("------delete image-----", "   is     -----------          YES ");
        }catch (Exception e){
            Log.d("------delete image-----", "   is     -----------          error ");
            e.printStackTrace();
        }finally {
            clossAll();  //关闭所有连接
        }
        return Row;
    }


    // 按照用户名查询用户是否有图片存着
    public String getname(String uname ){
        String item ="";
        try{
            getConnection();
            String sql="select * from image where name=?";
            pStmt = (PreparedStatement) conn.prepareStatement(sql);
            pStmt.setString(1,uname);

            rs = pStmt.executeQuery();
            if(rs.next()){
                item = uname;
            }

        }catch (Exception e){
            Log.d("getname", "error ");
            e.printStackTrace();
        }finally {
            clossAll();  //关闭所有连接
        }
        return item;
    }

}
